<template>
	<view class="paper-container">
		<!-- 顶部标题 -->
		<view class="page-header">
			<text class="page-title">试卷中心</text>
		</view>
		
		<!-- Tab切换 -->
		<view class="tab-bar">
			<view class="tab-item" 
				:class="{ active: activeTab === index }" 
				v-for="(tab, index) in tabs" 
				:key="index"
				@click="switchTab(index)">
				<text>{{ tab }}</text>
				<view class="tab-line" v-if="activeTab === index"></view>
			</view>
		</view>
		
		<!-- 试卷列表 -->
		<scroll-view class="paper-list" scroll-y @scrolltolower="loadMore" lower-threshold="100">
			<view class="paper-item" v-for="item in paperList" :key="item.id" @click="goToExam(item)">
				<view class="paper-header">
					<text class="paper-title">{{ item.title }}</text>
					<view class="time-badge">
						<text>{{ item.duration }}</text>
					</view>
				</view>
				
				<view class="paper-info">
					<text class="info-label">学科:{{ item.subject }}</text>
				</view>
				
				<view class="paper-info">
					<text class="info-text">题目数量:{{ item.questionCount }}</text>
				</view>
				
				<view class="paper-info">
					<text class="info-text">试卷总分:{{ item.totalScore }}</text>
				</view>
			</view>
			
			   <!-- 空状态 -->
			   <view class="empty-state" v-if="paperList.length === 0">
				   <image src="/image/dingdan.png" mode="aspectFit"></image>
				   <text>暂无试卷</text>
			   </view>
			   <!-- 加载更多提示 -->
			   <view v-if="paperList.length > 0" style="text-align:center;padding:30rpx 0;color:#999;">
				   <text v-if="loadingMore">加载中...</text>
				   <text v-else-if="noMore">没有更多了</text>
				   <text v-else>上拉加载更多</text>
			   </view>
		   </scroll-view>
	</view>
</template>

<script>
import { api } from '@/http.js'

export default {
	   data() {
		   return {
			   activeTab: 0,
			   tabs: ['固定试卷', '时段试卷', '班级试卷'],
			   paperList: [],
			   loading: false,
			   isLogin: false,
			   // 分页相关
			   pageNum: 1,
			   pageSize: 10,
			   noMore: false,
			   loadingMore: false
		   }
	   },
	   onShow() {
		   // 检查登录状态
		   const token = uni.getStorageSync('token');
		   this.isLogin = !!token;
		   if (this.isLogin) {
			   this.pageNum = 1;
			   this.noMore = false;
			   this.paperList = [];
			   this.getPaperList();
		   } else {
			   uni.showToast({
				   title: '请先登录',
				   icon: 'none'
			   });
			   setTimeout(() => {
				   uni.reLaunch({
					   url: '/pages/login/login'
				   });
			   }, 1500);
		   }
	   },
	   methods: {
		   async getPaperList(isLoadMore = false) {
			   if (isLoadMore) {
				   this.loadingMore = true;
			   } else {
				   this.loading = true;
			   }
			   try {
				   const paperType = this.activeTab + 1;
				   const res = await api.getPaperList({
					   paperType: paperType,
					   pageIndex: this.pageNum,
					   pageSize: this.pageSize
				   });
				   if (res && res.code === 1) {
					   if (res.response && res.response.list && res.response.list.length > 0) {
						   const newList = res.response.list.map(item => ({
							   id: item.id,
							   title: item.name,
							   subject: item.subjectName || '未知',
							   questionCount: item.questionCount || 0,
							   totalScore: item.score || 0,
							   duration: item.suggestTimeStr || '未知'
						   }));
						   if (isLoadMore) {
							   this.paperList = [...this.paperList, ...newList];
						   } else {
							   this.paperList = newList;
						   }
						   // 判断是否还有更多
						   if (newList.length < this.pageSize) {
							   this.noMore = true;
						   }
					   } else {
						   if (!isLoadMore) this.paperList = [];
						   this.noMore = true;
					   }
				   } else {
					   uni.showToast({
						   title: res?.message || '获取试卷列表失败',
						   icon: 'none'
					   });
				   }
			   } catch (err) {
				   uni.showToast({
					   title: '加载失败，请稍后重试',
					   icon: 'none'
				   });
			   } finally {
				   this.loading = false;
				   this.loadingMore = false;
			   }
		   },
		   loadMore() {
			   if (this.loadingMore || this.noMore || this.loading) return;
			   this.pageNum++;
			   this.getPaperList(true);
		   },
		   switchTab(index) {
			   this.activeTab = index;
			   this.pageNum = 1;
			   this.noMore = false;
			   this.paperList = [];
			   this.getPaperList();
		   },
		   goToExam(item) {
			   uni.navigateTo({
				   url: `/pages/exam/detail?paperId=${item.id}`
			   });
		   }
	   }
}
</script>

<style lang="scss" scoped>
.paper-container {
	min-height: 100vh;
	background: #f5f5f5;
	
	.page-header {
		background: #fff;
		padding: 30rpx;
		text-align: center;
		
		.page-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}
	}
	
	.tab-bar {
		display: flex;
		background: #fff;
		padding: 0 30rpx;
		
		.tab-item {
			flex: 1;
			text-align: center;
			padding: 30rpx 0;
			position: relative;
			
			text {
				font-size: 28rpx;
				color: #666;
			}
			
			&.active {
				text {
					color: #00BCD4;
					font-weight: bold;
				}
			}
			
			.tab-line {
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);
				width: 60rpx;
				height: 6rpx;
				background: #00BCD4;
				border-radius: 3rpx;
			}
		}
	}
	
	.paper-list {
		height: calc(100vh - 180rpx);
		padding: 0;
		
		.paper-item {
			margin: 20rpx 30rpx 0 30rpx;
			width: calc(100% - 60rpx);
			background: #fff;
			border-radius: 15rpx;
			padding: 30rpx;
			box-sizing: border-box;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
			
			.paper-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;
				
				.paper-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					flex: 1;
				}
				
				.time-badge {
					padding: 8rpx 20rpx;
					background: #FFE4E1;
					border-radius: 30rpx;
					
					text {
						font-size: 24rpx;
						color: #FF6B6B;
					}
				}
			}
			
			.paper-info {
				margin-bottom: 12rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.info-label {
					font-size: 26rpx;
					color: #999;
				}
				
				.info-text {
					font-size: 26rpx;
					color: #666;
				}
			}
		}
		
		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin: 200rpx 30rpx;
			padding: 0;
			width: calc(100% - 60rpx);
			box-sizing: border-box;
			
			image {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 30rpx;
				opacity: 0.3;
			}
			
			text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
}
</style>
